//树
var ua = navigator.userAgent;
var ipad = ua.match(/(iPad).*OS\s([\d_]+)/),
	isIphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/),
	isAndroid = ua.match(/(Android)\s+([\d.]+)/),
	isMobile = isIphone || isAndroid;
var layer,userMessageList = [{title:'质量',content:'你有新的消息!'}];
layui.use(['layer'], function(){
	layer = layui.layer;
});
$(function() {
	$.post(host + 'loginController/getMenus.html', {}, function(result) {
		var data = result.data;
		var nodes = [];
		var map = {};
		var menuMap = {};
		for (var i = 0;i < data.length;i ++) {
			if (data[i].parentId && data[i].parentId != "0") {
				if (!menuMap[data[i].parentId]) {
					menuMap[data[i].parentId] = [];
				}
				menuMap[data[i].parentId].push(data[i]);
			}
		}
		for (var i = 0;i < data.length;i ++) {
			map[data[i].id] = data[i];
			if (!menuMap[data[i].parentId]) {
				if (menuMap[data[i].id]) {
					map[data[i].id].children = menuMap[data[i].id];
				}
				nodes.push(map[data[i].id]);
			}
		}
		//createMenu("menu", nodes);
		
		layui.use('element', function(){
			var element = layui.element;
		});
		
		$.post(host + 'loginController/getUserInfo.html', {}, function(data) {
			if (data.status == "ok") {
				$("#userName").html(data.msg);
				var mainFrameSrc = checkUserType(data.userType);
				$("#mainFrame").attr("src", mainFrameSrc);//默认显示
			}
		}, "json");
	}, "json");

	$('#toggle').css("line-height", (document.documentElement.clientHeight - 60) + "px");
	//顶部左侧导航控制开关
	$('#toggle').click(function () {
		var side = $("#layside");
		var body = $("#laybody");
		var sideWidth = side.width();
		if (sideWidth > 100) {
			body.animate({
				left: '0',
				width: document.documentElement.clientWidth
			});
			side.animate({
				width: '0'
			});
			$(this).removeClass("triangle");
		} else {
			body.animate({
				left: '200px',
				width: document.documentElement.clientWidth - 200
			});
			side.animate({
				width: '200px'
			});
			$(this).addClass("triangle");
		}
	});
	layui.use(['form'], function() {
		layer = layui.layer;
	});
});
var ii;
function openScView(page){
	var names = ['工序看板','质量看板','工艺看板','设备看板','制造看板','图形看板'];
	var urls = ['mes/processReportPage_zr_new.html','mes/qualityReportPage_new.html','mes/technologyReportPage_new.html','mes/equipmentReportPage_new.html','mes/manufactureReportPage_new.html','mes/leaderChartReportPage.html'];
	$.post(host + 'mainReportPage/checkUser', {}, function(data) {
		if(data.flag){
			layui.use('layer', function(){
			  	var layer = layui.layer;
			  	ii = layer.open({
			  	  title:names[page],
				  type: 2, 
				  area: ['100%', '100%'],
				  content:urls[page]
				}); 
				layer.full(ii);
			});
		}
	}, "json");
}
function closeWin(index){
	layui.use('layer', function(){
	  	var layer = layui.layer;
	  	layer.close(index);
	});
}
//根据用户类型展示
function checkUserType(userType){
	var names = ['工序看板','质量看板','工艺看板','设备看板','制造看板','图形看板'];
	var urls = ['mes/processReportPage_zr_new.html','mes/qualityReportPage_new.html','mes/technologyReportPage_new.html','mes/equipmentReportPage_new.html','mes/manufactureReportPage_new.html','mes/leaderChartReportPage.html'];
	if(userType=='工序'){
		return urls[0];
	}
	if(userType=='质量'){
		return urls[1];
	}
	if(userType=='工艺'){
		return urls[2];
	}
	if(userType=='设备'){
		return urls[3];
	}
	if(userType=='制造'){
		return urls[4];
	}
	if(userType=='图形'){
		return urls[5];
	}
	return 'main.jsp';
}
function createMenu(id, list) {
	var html = [];
	html.push('<ul class="layui-nav layui-nav-tree site-demo-nav">');
	for (var i = 0;i < list.length;i ++) {
		html.push('<li class="layui-nav-item ' + (i == 0 ? "layui-nav-itemed" : "") + '">');
		html.push('<a href="javascript: ;">' + list[i].name + '</a>');
		if (list[i].children) {
			var children = list[i].children;
			html.push('<dl class="layui-nav-child">');
			for (var j = 0;j < children.length; j++) {
				html.push('<dd style="padding-left: 15px;"><i class="layui-icon" style="font-size: 12px; color: #FFF;float: left;margin-top: 2px;">&#xe623;</i>' +
						'<a href="javascript: void(0);" onclick="linkMenu(\'' + children[j].url + '\', \'' + children[j].name + '\',\''+ children[j].icon +'\');return false;" style="padding-left: 2px;">' + children[j].name + '</a></dd>');
			}
			html.push('</dl>');
		}
		html.push('</li>');
	}
	$("#" + id).html(html.join(""));
}
function linkMenu(url, title, icon) {
	$("#mainFrame").attr("src",url);
	$("#mainTitleImg").attr("src",icon);
	$("#mainTitleSpan").html(title);
}
//动态添加选项卡
function addTab(title, url, hideClose){
	if ($('#tt').tabs('exists', title)){
		$('#tt').tabs('select', title);
	} else {
		var content = '<iframe scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:100%;"></iframe>';
		$('#tt').tabs('add',{
			title: title,
			content: content,
			closable: hideClose ? false : true
		});
	}
}
var click_menu = null;
var closeOtherPanel = true;
function addAccordion(node, id, width) {
	if (node.children && node.children.length > 0) {
		$("#" + id).accordion('add', {
			title: "<div style='background: url(../script/images/directory.png) no-repeat;padding-left: 18px;'>" + node.name + "</div>",
			content: '<div style="width: ' + (width - 2) + 'px;overflow: hidden;" id="menu_' + node.id + '"></div>',
			selected: closeOtherPanel
		});
		$('#menu_' + node.id).accordion({
			animate: true,
			width: width - 8,
			border: false,
			height: node.children.length * 25 + 3,
			selected: false
		});
		for (var i = 0;i < node.children.length;i ++) {
			addAccordion(node.children[i], "menu_" + node.id, width - 8);
		}
	} else {
		var link = $('<div style="cursor: pointer;border-bottom: 1px #BADAFE solid;' +
			'padding-left: 25px;width: 100%;line-height: 25px;color: #0055FF;" ' +
			'onclick="addTab(\'' + node.name + '\', \'' + host + node.url + '\');">' + node.name + '</div>');
		$("#" + id).append(link);
		link.on({
			click: function() {
				if (click_menu) {
					$(click_menu).css({"background-color": "#ffffff", "font-weight": "normal"});
				}
				click_menu = this;
				$(this).css({"background-color": "#c0e0FF", "font-weight": "bold"});
			},
			mouseover: function() {
				if (click_menu != this) {
					$(this).css("background-color", "#E0ECFF");
				}
			},
			mouseout: function() {
				if (click_menu != this) {
					$(this).css("background-color", "#ffffff");
				}
			}
		});
	}
}
//退出系统
function confirm1(){
	layer.open({
		type: 1,
		title: false,//不显示标题栏
		closeBtn: false,
		area: '300px;',
		shade: 0.8,
		id: 'LAY_layuipro',//设定一个id,防止重复弹出
		btn: ['残忍退出', '继续使用'],
		content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">' +
			'确定退出系统？</div>',
		yes: function(index, layero){
			jQuery.post(host + 'loginController/loginOut.html', {}, function(data) {
				location.href = 'login.jsp';
			}, "json");
		}
	});
}
var dialog;
//修改密码窗口控制
function updatePassword(){  //打开窗口
	dialog = baseJs.window(host + "common/password.html", {
		title: "修改密码", width: 300, height: 200
	});
}

//跳转到首页
function goToHome(wn){
	$('#mainFrame').attr('src', 'main.jsp');
}

//处理session过期后刷新页面(监听周期120秒钟)
setInterval(function(){
    var ajaxTimeOut = $.ajax({
        url:'./loginController/timeout',
        type : 'POST',
        data :{},
        dataType:'json',
        success:function(data){
        	if(data){
                location.reload();
			}
        }
    });
}, 120000);

//显示消息提示框
function showMessage(){
	layer.closeAll();
	var tabs = [];
	for(var i=0;i<userMessageList.length;i++){
		tabs.push({title:userMessageList[i].title,content:'<span style="color:black">'+userMessageList[i].content+'</span>'});
	}
	layer.tab({
		area: isMobile ? ['300px', '200px'] : ['400px', '300px'],
		type: 1,
		shade: 0,
		offset:isMobile ? 't' : 'rb',
		anim: isMobile ? 1 : 2,
		tab: tabs,
		cancel: function(index, layero){
			var messageIdArr = [];
			for(var i=0;i<userMessageList.length;i++){
				messageIdArr.push(userMessageList['MESSAGEID']);
			}
			if(confirm('确定要关闭么')) {
				$.ajax({
					url:'./message/updateMessageStatusByUser',
					type : 'POST',
					data :{messageIds:messageIdArr.join(",")},
					dataType:'json',
					success:function(data){
					}
				});
				layer.close(index);
			}
			return false;
		}
	});
}